﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Common;
using WebSite.Core;

namespace WebSite.Server
{
    /// <summary>
    /// AcceptTeam 的摘要说明
    /// </summary>
    public class AcceptTeam : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {

        public void ProcessRequest( HttpContext context )
        {
            context.Response.ContentType = "text/json";

            RetInfo response = RetInfoHelper.WebServiceReturn( delegate()
            {
                int curShortMsgID = StringHelper.Convert( context.Request["shortmsgid"], 0 );

                if (Project_Session.CurrentUser == null || Project_Session.CurrentUserBaby == null)
                {
                    throw new RuntimeException( 3, "请先完善宝宝资料" );
                }

                if (NullableHelper.CastValueType( Project_Session.CurrentUserBaby.ConfirmTeamId ) != 0)
                {
                    throw new RuntimeException( 10, "您的宝宝已经组队" );
                }

                if (curShortMsgID == 0)
                {
                    throw new RuntimeException( 11, "无效的组队邀请" );
                }

                Maticsoft.BLL.CarrefourCover.ShortMessage shortMsgbll = new Maticsoft.BLL.CarrefourCover.ShortMessage();
                var shortMsgModel = shortMsgbll.GetModel( curShortMsgID );
                if (shortMsgModel == null || shortMsgModel.MsgType != 1 || shortMsgModel.RecevieBabyId != Project_Session.CurrentUser.UserId)
                {
                    throw new RuntimeException( 11, "无效的组队邀请" );
                }

                // 检测来源
                Maticsoft.BLL.CarrefourCover.Baby babybll = new Maticsoft.BLL.CarrefourCover.Baby();
                var srcbabymodel = babybll.GetModel( NullableHelper.CastValueType( shortMsgModel.SendBabyId ) );
                if (srcbabymodel == null || NullableHelper.CastValueType( srcbabymodel.ConfirmTeamId ) != 0)
                {
                    throw new RuntimeException( 22, "对方宝宝信息不存在或已经组队" );
                }

                // 生成组队图片
                string teamImage = TeamHelper.CombinTeamHead( srcbabymodel.BabyHeadImg, Project_Session.CurrentUserBaby.BabyHeadImg );

                // 组队
                Maticsoft.BLL.CarrefourCover.Team teambll = new Maticsoft.BLL.CarrefourCover.Team();

                var newteamid = teambll.Add( new Maticsoft.Model.CarrefourCover.Team()
                {
                    BabyAId = srcbabymodel.Id,
                    BabyAName = srcbabymodel.BabyName,
                    BabyBId = Project_Session.CurrentUserBaby.Id,
                    BabyBName = Project_Session.CurrentUserBaby.BabyName,
                    Status = 1,
                    TeamHeadImg = teamImage,
                    TeamName = srcbabymodel.BabyTeamName,
                    TeamVoteNum = 0,
                    CreateTime = DateTime.Now,
                    Ip = context.Request.UserHostAddress
                } );

                // 更新baby的冗余信息
                var currentbabymodel = Project_Session.CurrentUserBaby;
                srcbabymodel.ConfirmTeamId = currentbabymodel.ConfirmTeamId = newteamid;
                srcbabymodel.ConfirmTeamName = currentbabymodel.ConfirmTeamName = srcbabymodel.BabyTeamName;
                babybll.Update( srcbabymodel );
                babybll.Update( currentbabymodel );

                // 将消息标记为已逻辑删除
                shortMsgModel.IsDelete = 1;
                shortMsgbll.Update( shortMsgModel );

                // 发送消息给来源
                shortMsgbll.Add( new Maticsoft.Model.CarrefourCover.ShortMessage()
                {
                    RecevieBabyId = srcbabymodel.Id,
                    SendBabyId = Project_Session.CurrentUser.UserId,
                    SendBabyName = Project_Session.CurrentUserBaby.BabyName,
                    SendBabyHeadImg = Project_Session.CurrentUserBaby.BabyHeadImg,
                    MsgType = 0,
                    MsgContent = "已接受了组队邀请",
                    IsRead = 0,
                    IsDelete = 0,
                    CreateTime = DateTime.Now,
                    Ip = context.Request.UserHostAddress
                } );

                Project_Session.RefreshBabyInfo();

                return true;
            } );

            context.Response.Write( response.SerializeToString( Common.Serialization.SerializationType.Json ) );
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}